var express = require('express');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json())
//设置跨域访问
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type, X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

app.get('/getUserInfo', function (req, res) {
    let id = req.query.id;
    let name, birthday;
    switch (id) {
        case '1': name = 'get1', birthday = '19910930'; break;
        case '2': name = 'get2', birthday = '19920930'; break;
        case '3': name = 'get3', birthday = '19930930'; break;
        default: name = 'no get', birthday = 'no birthday'; break;
    }
    let resData = JSON.stringify({
        name: name,
        birthday: birthday
    });
    res.send(resData);
})

app.post('/postUserInfo', function (req, res) {
    console.log(req.body)
    let id = req.body.id;
    let name, birthday;
    switch (id) {
        case '1': name = 'post1', birthday = '19910930'; break;
        case '2': name = 'post2', birthday = '19920930'; break;
        case '3': name = 'post3', birthday = '19930930'; break;
        default: name = 'no post', birthday = 'no birthday'; break;
    }
    let resData = JSON.stringify({
        name: name,
        birthday: birthday
    });
    res.send(resData);
})

var server = app.listen(8081, function () {

    var host = server.address().address
    var port = server.address().port

    console.log("应用实例，访问地址为 http://%s:%s", host, port)

})